package adke.managers;

import adke.gui.panels.ScoreHolderPanel;
import adke.listeners.GameListener;
import adke.listeners.PlayerListener;
import adke.models.Player;
import adke.models.ScoreTable;

// beheer huidige scores en all-time high?

// scores hier bijhouden of in Player zelf?

public class ScoreManager implements PlayerListener, GameListener
{
    private static ScoreManager reference;
    
    // houd de scores onderling bij.
    // zo kunnen we achteraf laten zien wie wie hoevaak gekilled heeft
    private static ScoreTable scoreTable;
    
    public ScoreManager()
    {
        reference = this;
    }
    
    public static ScoreManager getInstance()
    {
        return reference;
    }
    
    public static void playerDied()
    {
        Player[] players = PlayerManager.getPlayers(true); // levend
        for (Player p : players)
        {
            p.increaseScore();
        }
        
        // refresh scores
        ScoreHolderPanel.getInstance().refresh();
    }
    
    // TODO: maak customizable
    public static int getEndScoreForPlayerCount(int playerCount)
    {
        int x = -1;
        return (playerCount + x) * 10; 
//        return 1;
    }
    
    public static int getPlayerCountWithScore(int score)
    {
        int playerCount = 0;
        
        Player[] players = PlayerManager.getPlayers();
        for (Player p : players)
        {
            if (p.getScore() == score)
                playerCount++;
        }
        
        return playerCount;
    }
    
    public static boolean scoreReached()
    {
        int endScore = getEndScoreForPlayerCount(PlayerManager.getPlayerCount());
        
        Player[] players = PlayerManager.getPlayers();
        for (Player p : players)
        {
            if (p.getScore() >= endScore)
                return true;
        }
        
        return false;
    }

    //
    
    public void playerDied(Player deadPlayer)
    {
        playerDied();
        scoreTable.playerDied(deadPlayer);
    }

    public void playerKilledPlayer(Player killer, Player victim)
    {
        playerDied();
        scoreTable.playerKilledPlayer(killer, victim);
    }
    
    public void gameStarted()
    {
        scoreTable = new ScoreTable();
        PlayerManager.addListener(this);
    }

    public void gameEnded()
    {
        
    }

    public void roundStarted()
    {
        
    }

    public void roundEnded()
    {
        
    }
}
